// 2013-7-28下午12:44:03  net.betou.trade.action.abs.ActionSupport.java

package net.betou.trade.action.abs;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import net.betou.common.page.Pagination;
import net.betou.trade.main.manager.LogMng;
import net.betou.trade.web.Messages;
import net.betou.trade.web.ResponseUtil;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;

/**
 * @author 叶装装
 */
public abstract class ActionSupport< T > {

	protected static final String PAGE_LIST = "/list";
	protected static final String PAGE_ADD = "/add";
	protected static final String PAGE_EDIT = "/edit";

	/**
	 * 将数据保存到model中，这些参数查询列表的时候都要用到的
	 * 
	 * @param model
	 * @param pageSize
	 *            显示多少条数据
	 * @param searchProperty
	 *            查找的KEY
	 * @param searchValue
	 *            查找的value
	 * @param orderProperty
	 *            排序的key
	 * @param orderDirection
	 *            升序还是降序
	 */
	protected void paramToModel( ModelMap model, Integer pageSize, String searchProperty, String searchValue, String orderProperty, String orderDirection ) {
		paramToModel( model, pageSize, searchProperty, searchValue, orderProperty, orderDirection, null );
	}

	/**
	 * @param model
	 * @param pageSize
	 * @param searchProperty
	 * @param searchValue
	 * @param orderProperty
	 * @param orderDirection
	 * @param filterStatus
	 *            过滤的条件
	 */
	protected void paramToModel( ModelMap model, Integer pageSize, String searchProperty, String searchValue, String orderProperty, String orderDirection, String filterStatus ) {
		model.addAttribute( "pageSize", pageSize );
		model.addAttribute( "orderDirection", orderDirection );
		model.addAttribute( "orderProperty", orderProperty );
		model.addAttribute( "searchProperty", searchProperty );
		model.addAttribute( "searchValue", searchValue );
		model.addAttribute( "filterStatus", filterStatus );
	}

	protected void paramToModel( ModelMap model, Pagination p, List< T > list, Integer pageSize, String searchProperty, String searchValue, String orderProperty, String orderDirection, String filterStatus ) {
		model.addAttribute( "pageSize", pageSize );
		model.addAttribute( "orderDirection", orderDirection );
		model.addAttribute( "orderProperty", orderProperty );
		model.addAttribute( "searchProperty", searchProperty );
		model.addAttribute( "searchValue", searchValue );
		model.addAttribute( "filterStatus", filterStatus );
		if( list != null ){
			model.addAttribute( "count", list == null ? 0 : list.size( ) );
			model.addAttribute( "list", list );
		}

		if( p != null ){
			model.addAttribute( "pagination", p );
		}
	}

	protected Set< String > splitPerms( String [ ] perms ) {
		Set< String > set = new HashSet< String >( );
		if( perms != null ){
			for ( String perm : perms ){
				for ( String p : StringUtils.split( perm, ',' ) ){
					if( !StringUtils.isBlank( p ) ){
						set.add( p );
					}
				}
			}
		}
		return set;
	}
	
	protected String clearHtml( String content ){
		if( StringUtils.isNotBlank( content )){
			content = Jsoup.clean( content, Whitelist.none( ) ); // 清除所有的HTML
		}
		return content;
	}

	protected ResponseUtil validateSmsSend( String groups, String content, HttpServletResponse response ){
		ResponseUtil util = ResponseUtil.create( response );
		
		if( StringUtils.isBlank( groups )){
			util.addErrorResponse(  Messages.SMS_NOT_GROUP   );
			return util;
		}
		
		if( StringUtils.isBlank(  content )){
			util.addErrorResponse(  Messages.SMS_NOT_CONTENT   );
			return util;
		}
		return util;
	}
	
	protected abstract String getDirectory( );

	public String getAddPage( ) {
		return getDirectory( ) + PAGE_ADD;
	}

	public String getListPage( ) {
		return getDirectory( ) + PAGE_LIST;
	}

	public String getUpdatePage( ) {
		return getDirectory( ) + PAGE_EDIT;
	}

	@Autowired
	protected LogMng logMng;

}
